<?php

namespace App\Library\Logic;

use app\Library\Requests\Base\BasePageRequest;
use App\Models\AuctionFollowModel;
use Illuminate\Database\Eloquent\Model;

class AuctionFollowLogic extends AuctionFollowModel
{
    public static function followOrCancel(Model $auction): void
    {
        $follow = self::query()
            ->where([
                'user_id' => USERID,
                'auction_id' => $auction->id
            ])->first();

        if (!is_null($follow)) self::query()->where(['user_id' => USERID,'auction_id' => $auction->id])->delete();
        else {
            self::insert([
                'user_id' => USERID,
                'auction_id' => $auction->id
            ]);
        }
    }

    public static function paging(BasePageRequest $request): array
    {
        $builder = self::query()->where('auction_follow.user_id', userId())
            ->leftJoin('auction', 'auction.id', 'auction_follow.auction_id');
        return paginate(helpBuilder($builder, $request)->orderByDesc('auction_follow.created_at')->paginate());
    }
}
